/**
 * 
 */
package com.Adapters;


import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import com.Adapters.Items.ItemAlbum;
import com.Adapters.Items.ItemPost;
import com.Animations.AnimationLists;
import com.Data.Albums;
import com.Init.ImageDownloaderInit;
import com.Managers.FragmentManagerClass;
import com.activities.AlbumFunctions;
import com.activities.DefaultFunctions;
import com.nostra13.universalimageloader.core.ImageLoader;


/**
 * @author Chris
 * 
 */
public class AlbumAdapter extends ArrayAdapter<Albums> implements OnScrollListener {

    private DefaultFunctions actionsMain;

    private AlbumFunctions funcs;

    private int imageWidth;

    private ImageLoader loader;

    private boolean loading;

    private int previousTotal;

    private ArrayList<ItemPost> rows;

    /**
     * @param context
     * @param textViewResourceId
     */
    public AlbumAdapter(int imageWidth, Context context,
                int textViewResourceId, DefaultFunctions actionsMain,
                AlbumFunctions funcs) {

        super(context, textViewResourceId);
        // TODO Auto-generated constructor stub
        rows = new ArrayList<ItemPost>();
        this.imageWidth = imageWidth;
        this.actionsMain = actionsMain;
        this.funcs = funcs;

        setOptions();
    }

    private void setOptions() {

        ImageDownloaderInit.config(getContext(), ".thumb/");
        this.loader = ImageLoader.getInstance();

    }

    @Override
    public void add(Albums object) {

        rows.add(new ItemAlbum(imageWidth, LayoutInflater.from(getContext()), object, this.actionsMain, this.funcs, loader));
    }

    @Override
    public void clear() {

        AnimationLists.displayedImages.clear();
        rows.clear();
    }

    public boolean contains(Albums object) {

        for (int i = 0; i < rows.size(); i++) {
            ItemPost p = rows.get(i);
            if (object.getID() == p.getPostId()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int getCount() {

        return rows.size();
    }

    @Override
    public int getItemViewType(int position) {

        return rows.get(position).getViewType();
    }

    public void updateItemAlbum(Albums album) {

        for (int i = 0; i < rows.size(); i++) {
            ItemAlbum p = (ItemAlbum) rows.get(i);
            if (album.getID() == p.getPostId()) {
                p.setAlbum(album);

                notifyDataSetChanged();

            }
        }
    }

    @Override
    public Albums getItem(int position) {

        return ((ItemAlbum) rows.get(position)).getAlbum();
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {

        return rows.get(position).getView(v, position);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        if (loading) {
            // if trying to loading

            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        // si se cargaron los items necesarios
        if (!(loading)
                    && (totalItemCount - visibleItemCount) <= (firstVisibleItem)) {

            if (getCount() > 1) {

                getMoreAlbums(totalItemCount);
            }
            //
            loading = true;
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

        if (loader != null) {
            switch (scrollState) {
                case OnScrollListener.SCROLL_STATE_IDLE:
                    loader.resume();
                    break;
                case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                    loader.pause();
                    break;
                case OnScrollListener.SCROLL_STATE_FLING:
                    loader.pause();
                    break;
            }
        }
    }

    // /////////////////////////////////////////////////
    // LIST METHODS
    // ///////////////////////////////////////////////

    @Override
    public void remove(Albums object) {

        for (int i = 0; i < rows.size(); i++) {
            ItemPost p = rows.get(i);
            if (object.getID() == p.getPostId()) {
                rows.remove(i);

                notifyDataSetChanged();

            }
        }
        if (getCount() == 0)
            FragmentManagerClass.aAlbum.setNoItemsPopUp();
    }

    public void setLoader(ImageLoader loader) {

        this.loader = loader;
    }

    /**
     * @param totalItemCount
     */
    private void getMoreAlbums(int totalItemCount) {

        // TODO Auto-generated method stub

    }

}
